package k.daniel.android.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import java.lang.reflect.Field;

/**
 * Created by daniel on 2015/4/27.
 */
public class ViewPagerScroller extends Scroller {

    private int mScrollDuration = 2000;// 滑动速度

    public ViewPagerScroller(Context context) {
        super(context);
    }

    public ViewPagerScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        super.startScroll(startX, startY, dx, dy, mScrollDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        super.startScroll(startX, startY, dx, dy, mScrollDuration);
    }

    public static void setScroller(ViewPager viewPager) {
            try {
                Field mScroller = null;
                mScroller = ViewPager.class.getDeclaredField("mScroller");
                mScroller.setAccessible(true);
                ViewPagerScroller scroller = new ViewPagerScroller(viewPager.getContext());
                mScroller.set(viewPager, scroller);
            } catch (Exception e) {
                Log.e("SET SCOLLER", "error of change scroller ", e);
            }
    }
}